package www.com.library.util;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * 文本框变动监视器
 *
 */
public class CommonTextWatcher implements TextWatcher {

	public final static int COMMON_TEXT_CLEANED = 100301;
	public final static int COMMON_TEXT_CHANGED = 100302;
	public final static int COMMON_TEXT_FOCUSED = 100303;
	public final static int COMMON_TEXT_UNFOCUSED = 100304;
	public final static int COMMON_TEXT_NOCONTENT = 100305;

	private ImageView textCleaner = null;
	private EditText mText = null;
	private Handler mHandler = null;
	private View mRootView = null;

	private CommonTextWatcher(View rootView, int textLayoutID, int cleanerLayoutID, Handler handle) {
		mHandler = handle;
		mRootView = rootView;

		initTextWatcher(textLayoutID, cleanerLayoutID);
	}

	/**
	 * 绑定文本框资源ID
	 */
	public static CommonTextWatcher bind(View rootView, int textLayoutID, int cleanerLayoutID) {
		return bind(rootView, textLayoutID, cleanerLayoutID, null);
	}

	/**
	 * 绑定文本框资源ID
	 */
	public static CommonTextWatcher bind(View rootView, int textLayoutID, int cleanerLayoutID, Handler handle) {
		CommonTextWatcher watcher = new CommonTextWatcher(rootView, textLayoutID, cleanerLayoutID, handle);

		if (null != watcher.mText) {
			watcher.mText.addTextChangedListener(watcher);
		}

		return watcher;
	}

	/**
	 * 绑定文本框
	 */
	public static CommonTextWatcher bind(EditText editor, int cleanerLayoutID) {
		return bind(editor, cleanerLayoutID, null);
	}

	/**
	 * 绑定文本框
	 */
	public static CommonTextWatcher bind(EditText editor, int cleanerLayoutID, Handler handle) {
		if (null == editor || null == editor.getParent()) {
			return null;
		}

		CommonTextWatcher watcher = new CommonTextWatcher((View) editor.getParent(), editor.getId(), cleanerLayoutID, handle);

		if (null != watcher.mText) {
			watcher.mText.addTextChangedListener(watcher);
		}

		return watcher;
	}

	/**
	 * 初始化文本监听器
	 */
	private void initTextWatcher(int textLayoutID, int cleanerLayoutID) {
		if (null == mRootView) {
			return;
		}

		mText = (EditText) mRootView.findViewById(textLayoutID);

		if (null == mText) {
			return;
		}

		textCleaner = (ImageView) mRootView.findViewById(cleanerLayoutID);

		if (null == textCleaner) {
			return;
		}

		textCleaner.setVisibility(View.GONE);

		textCleaner.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (textCleaner == v) {
					mText.setText("");
					if (null != mHandler) {
						mHandler.sendEmptyMessage(COMMON_TEXT_CLEANED);
					}
				}
			}
		});
	}

	/**
	 * 关键字变动后，给UI发送消息
	 */
	@Override
	public void afterTextChanged(Editable s) {
		if (mText.hasFocus() && mText.getText().toString().trim().length() > 0) {
			textCleaner.setVisibility(View.VISIBLE);
		} else {
			textCleaner.setVisibility(View.GONE);
		}
		if (null != mHandler) {
			mHandler.sendEmptyMessage(COMMON_TEXT_CHANGED);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}
}
